package com.android.calculator;

import com.jy.appmarket.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class InputButton extends RelativeLayout implements OnClickListener {
	private static final int DEFAULT_IMAGE_BG = R.color.transparent;
	private int mSrcId;
	ImageView mImageView;
	OnClickListener mListener;

	public InputButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		CalculatorActivity calc = (CalculatorActivity) context;
		mListener = calc.mListener;
		init(context, attrs);
		setOnClickListener(this);
	}

	public void onClick(View view) {
		mListener.onClick(this);
	}

	private void init(Context context, AttributeSet attrs) {
		if (attrs != null) {
			TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.CalculatorView);
			mSrcId = array.getResourceId(R.styleable.CalculatorView_src, mSrcId);
			array.recycle();
		}
		setGravity(Gravity.CENTER);
		addView(createImageView());
	}

	private View createImageView() {
		mImageView = new ImageView(getContext());
		mImageView.setBackgroundResource(DEFAULT_IMAGE_BG);
		mImageView.setImageResource(mSrcId);
		LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		mImageView.setLayoutParams(lp);
		return mImageView;
	}

}
